package com.tengju.bff.interfaces.app.charge;

import com.tengju.bff.interfaces.app.charge.facade.MarketFacade;
import com.tengju.bff.interfaces.app.charge.vo.*;
import com.tengju.bff.interfaces.app.user.vo.RangeVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.CastUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author chenhang
 * @Date 2021/1/26
 */
@RestController
@Api(tags = "腾炬市场指数分析接口")
@RequestMapping(value = "market")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class MarketController {

    private final MarketFacade marketFacade;

    @ApiOperation(value = "市场指数分析接口", notes = "市场指数分析接口")
    @GetMapping(value = "analysis", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MonthMarketAnalysisVO> analysis(@RequestParam("month") String month) {
        try {

            UserServletContext servletContext = ServletContextUtil.getServletContext();

            Long idCodeValue = servletContext.needIdCode().getValue();


            return ApiResponse.newSuccess(marketFacade.analysis(UserIdCode.of(idCodeValue), CastUtil.castInt(month.replaceAll("-",""))));
        } catch (InterfaceException e) {
            log.error("市场指数分析接口异常",e);
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "市场占比详情接口", notes = "市场占比详情接口")
    @GetMapping(value = "rateDetail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MonthMarketRateDetailVO> rateDetail(@RequestParam("month") String month) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();

            Long idCodeValue = servletContext.needIdCode().getValue();

            return ApiResponse.newSuccess(marketFacade.rateDetail(UserIdCode.of(idCodeValue), CastUtil.castInt(month.replaceAll("-",""))));
        } catch (InterfaceException e) {
            log.error("市场占比详情接口",e);
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "市场指数分类指标占比", notes = "市场指数分类指标占比")
    @RequestMapping(value = "month/market/classify/ratio", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<MonthMarketClassifyVO> getMonthMarketClassifyRatio(@RequestParam("month") String month) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        Long idCodeValue = servletContext.needIdCode() == null ? null : servletContext.needIdCode().getValue();

        return ApiResponse.newSuccess(marketFacade.getMonthMarketAnalysis(month,UserIdCode.of(idCodeValue)));
    }

    @ApiOperation(value = "广告指数分类指标占比", notes = "广告指数分类指标占比")
    @RequestMapping(value = "month/advertise/classify/ratio", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<MonthMarketClassifyVO> getMonthAdvertiseClassifyRatio(@RequestParam("month") String month) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        Long idCodeValue = servletContext.needIdCode() == null ? null : servletContext.needIdCode().getValue();

        return ApiResponse.newSuccess(marketFacade.getMonthAdvertiseAnalysis(month,UserIdCode.of(idCodeValue)));
    }



    @ApiOperation(value = "每天新增市场指数趋势图", notes = "每天新增市场指数趋势图")
    @RequestMapping(value = "month/date/market/tendency", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<RangeVO>> getMonthDateMarketTendency (@RequestParam("month") String month) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        Long idCodeValue = servletContext.needIdCode() == null ? null : servletContext.needIdCode().getValue();

        return ApiResponse.newSuccess(marketFacade.getMonthDateMarketTendency(month,UserIdCode.of(idCodeValue)));
    }


    @ApiOperation(value = "月份每天市场指数详情", notes = "月份每天市场指数详情")
    @RequestMapping(value = "month/date/market/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<DateMarketClassifyVO>> getMonthDateMarketDetail(@RequestParam("month") String month) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        Long idCodeValue = servletContext.needIdCode() == null ? null : servletContext.needIdCode().getValue();

        return ApiResponse.newSuccess(marketFacade.getMonthDateMarketDetail(month,UserIdCode.of(idCodeValue)));

    }

    @ApiOperation(value = "每月新增市场指数趋势图", notes = "每月新增市场指数趋势图")
    @RequestMapping(value = "year/month/market/tendency", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<RangeLineVO> getYearMonthMarketTendency (@RequestParam("year") String year) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        Long idCodeValue = servletContext.needIdCode() == null ? null : servletContext.needIdCode().getValue();

        return ApiResponse.newSuccess(marketFacade.getYearMonthMarketTendency(year,UserIdCode.of(idCodeValue)));
    }

    @ApiOperation(value = "经营指数结算周期提示文案", notes = "经营指数结算周期提示文案")
    @RequestMapping(value = "index/settle/cycle/hint", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<KVVO> getIndexSettleCycleHint(@RequestParam("month") String month) {

        return ApiResponse.newSuccess(marketFacade.getIndexSettleCycleHint(month));
    }

}
